package com.yumingjaing.controller;

import com.yumingjaing.cache.User;
import com.yumingjaing.listener.QueryUserSuccessEvent;
import com.yumingjaing.service.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description:
 * @author: John
 * @version: UserController.java, v1.0 2024年12月14日 03:46
 */
@RestController
@RequestMapping("/u")
public class UserController implements ApplicationEventPublisherAware {

    /**
     * 事件发布者
     */
    private ApplicationEventPublisher eventPublisher;

    @Autowired
    private UserServiceImpl userService;

    @GetMapping("/q")
    public User query(String name){
        User user = userService.queryUserByName(name);
        if (user!=null){
            this.eventPublisher.publishEvent(new QueryUserSuccessEvent(this,user));
        }
        return user;
    }

    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher eventPublisher) {
        this.eventPublisher=eventPublisher;
    }
}